#include <stdio.h>
#include <stdlib.h>
#include "tsmatrix.h"

// You should sort this f**king data
void sortData(Triple* data, int n) {
    for(int i=0;i<n;i++) {
        for(int j=1;j<n;j++) {
            if((data[j].i < data[j-1].i) || (data[j].i == data[j-1].i && data[j].j < data[j-1].j)) {
                Triple tmp = data[j];
                data[j] = data[j-1];
                data[j-1] = tmp;
            }
        }
    }
}

bool add_matrix(const TSMatrix *pM, const TSMatrix *pN, TSMatrix *pQ){
    if(pM -> n != pN -> n || pM -> m != pN -> m) return false;
    pQ -> n = pM -> n; pQ -> m = pM -> m; pQ -> len = pM -> len;
    for(int i=0;i<pM -> len;i++) 
        pQ -> data[i] = pM -> data[i];
    for(int i=0;i<pN -> len;i++) {
        bool found = false;
        for(int j=0;j<pQ -> len;j++) {
            if(pN -> data[i].i == pQ -> data[j].i && pN -> data[i].j == pQ -> data[j].j) {
                found = true;
                pQ -> data[j].e += pN -> data[i].e;
                if(pQ -> data[j].e == 0) {
                    for(int k=j;k<pQ->len;k++) pQ -> data[k] = pQ -> data[k+1];
                    --pQ -> len;
                }
            }
        }
        if(!found) pQ -> data[pQ -> len ++] = pN -> data[i];
    }
    sortData(pQ -> data, pQ -> len);
    return true;
}


// ------------
// |   TEST   | 
// ------------ 

int main() {
    int n, m; scanf("%d %d", &n, &m);
    TSMatrix a = constructMatrix(n, m), b = constructMatrix(n, m), c;
    print(a); putchar('\n'); print(b); putchar('\n');
    add_matrix(&a, &b, &c); print(c);
}